#!/usr/bin/perl
# SCRIPT PAGE
use strict;
use warnings;
require 'forum_common.pl';

connect_to_database() or bad_deal();
my $user = get_current_user(get_sess_id());

record_this_hit();

my $board_id = url_param('board');
bad_deal('No board specified!') unless (defined $board_id);


my $board;
bad_deal() unless ($board = get_board($board_id) and
					!($forum::errmsg = cant_maketopic($user, $board)));
my $board_title = escapeHTML($board->{TITLE});

my ($msg, $title, $descr, $content) = ('');
if (defined($title = param('title')) &&
	defined($descr = param('descr')) &&
	defined($content = param('content')) &&
	$ENV{'REQUEST_METHOD'} eq 'POST')
{
	my ($submit, $topic_id, $post_id);
	if (defined($submit = param('submit')) && $submit eq $forum::preview_text)
	{
		if (my @errs = verify_topic($title, $descr, $content))
		{
			$msg = forum_error('', join br(), @errs);
		}
		else
		{
			$msg = '';
		}
		$msg .= div({-id=>'post_preview'}, as_post($content));
	}
	elsif(($topic_id,$post_id)=make_topic($board,$user,$title,$descr,$content))
	{
		print redirect(-status=>303,
						-location=>"topic.cgi?id=$topic_id#post$post_id");
		exit;
	}
	else
	{
		$msg = nl2br(forum_error());
	}
}

print header($forum::http_header), forum_start(forum_cpanel($user)),
	"Board: $board_title", br(),
	br(),
	$msg,
	start_form(-action=>'', -method=>'post'),
	'<fieldset id="maketopic">',
	legend('New Topic'),
	'Title: (max 64 characters)', br(),
	textfield(-name=>'title', -size=>32, -maxlength=>64), br(),
	'Description: (max 128 characters)', br(),
	textfield(-name=>'descr', -size=>64, -maxlength=>128), br(),
	'Post Content: (max 15000 characters)', br(),
	textarea(-name=>'content', -cols=>100, -rows=>20, -id=>'postcontent'),
	div({-class=>'subbtn'},
		submit(-name=>'submit', -value=>'Preview'),
		submit(-name=>'submit', -value=>'Create Topic')),
	'</fieldset>',
	end_form();


gameover:
print forum_end();
forum_disconnect();
